package context;

import entity.pojo.TokenInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@Component
@RequiredArgsConstructor
public class WxContext {
    public static final String APPID = "wxd783d7acea1c6203";
    public static final String SECRETE = "6d1ad8d79c43f1a8fdb1ef73ade9364a";
    public static final String WX_LOGIN_CALLBACK_URL = "https://1ad86c33.r18.cpolar.top/user/user/wxCallback";
    public static final String WX_LOGIN_QRCODE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize";
    public static final String TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
    public static final String GET_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo";

    public static String spliceWxLoginQRCodeUrl(String sid) {
        return WX_LOGIN_QRCODE_URL +
                "?appid=" + APPID +
                "&redirect_uri=" + URLEncoder.encode(WX_LOGIN_CALLBACK_URL + "/" + sid, StandardCharsets.UTF_8) +
                "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    }

    public static String spliceTokenUrl(String code) {
        return TOKEN_URL +
                "?appid=" + APPID +
                "&secret=" + SECRETE +
                "&code=" + code +
                "&grant_type=authorization_code";
    }

    public static String spliceGetUserInfoUrl(TokenInfo tokenInfo) {
        return GET_USERINFO_URL + "?access_token=" + tokenInfo.getAccess_token()
                + "&openid=" + tokenInfo.getOpenid() + "&lang=zh_CN";
    }


}
